package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("新增图书....");
        //判满
        int size = bookList.getBooksize();
        if(size == bookList.getBooks().length){
            System.out.println("书架满了，不能放了！");
            return;
        }
        //输入新增图书的属性
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入图书的名字：");
        String name = scanner.nextLine();
        System.out.printf("请输入图书的作者：");
        String author = scanner.nextLine();
        System.out.printf("请输入图书的类型：");
        String type = scanner.nextLine();
        System.out.printf("请输入图书的价格：");
        int price = scanner.nextInt();
        Book newbook = new Book(name,author,price,type);
        //查重
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("有这本书了！");
                System.out.println(book);
                return;
            }
        }
        //插入
        bookList.setBook(size,newbook);
        //有效图书个数+1!!!!!
        bookList.setBooksize(size+1);
        System.out.println("新增成功！");
    }
}
